﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.Model;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using FineUI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Data;
namespace IHISCETeim.Web.AgentSys.SysMgr
{
    public partial class AgentUserEdit : AgentPageBase
    {


        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.修改 };
            }
        }
        protected override string DWZRef
        {
            get { return "AgentUserList"; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetConfirmHideReference();
            CheckPowerWithButton(Permission.修改, this.btnSaveClose);

            int UserId = GetRequestValue<int>("userid", 0);
            OUserInfo objUser = OUserController.GetById(UserId);
            if (objUser == null)
            {
                // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                Alert.Show("用户不存在,参数错误！", String.Empty, ActiveWindow.GetHideReference());
                return;
            }
            //// 初始化用户所属部门　经销商
            InitUserDept();
            InitRoles();

            this.tbSelectedDept.Text = objUser.Department;
            this.tbxUserName.Text = objUser.UserName;
            this.tbxPassword.Text = objUser.PassWord;
           // this.tbxRealName.Text = objUser.RealName;
            this.cbxState.Checked = objUser.State == 1;
            this.tbxEmail.Text = objUser.EMail;
           // this.tbxPartsEmail.Text = objUser.PartsEmail;
            this.tbxMobile.Text = objUser.Mobile;
            this.ddlRoleId.SelectedValue = objUser.RoleId.ToString();
            this.ddlSrvActUserId.SelectedValue = objUser.SrvActUserId.ToString();
           // this.tbxRemark.Text=objUser.r
            this.hfSelectedDept.Text = objUser.Department;
        }
        #region InitUserRole

        private void InitUserDept()
        {
            // 打开编辑窗口
            string selectDeptURL = String.Format("./SelectDept.aspx?ids=<script>{0}</script>", hfSelectedDept.GetValueReference());
            tbSelectedDept.OnClientTriggerClick = Window1.GetSaveStateReference(hfSelectedDept.ClientID, tbSelectedDept.ClientID)
                    + Window1.GetShowReference(selectDeptURL, "选择经销商");

        }

        private void InitRoles()
        {
            this.ddlRoleId.DataSource = ORoleController.GetAll().Select("IsAgent=1").ToList();
            this.ddlRoleId.DataBind();

            //this.ddlSrvActUserId.DataSource = OUserController.GetUserByDepartment("'售后服务部'");
            //this.ddlSrvActUserId.DataBind();
            List<AgentPowerConfigInfo> list = new List<AgentPowerConfigInfo>();
            GetPowerList(list, 0);
            this.ddlSrvActUserId.DataSource = list;
            this.ddlSrvActUserId.DataBind();
        }
       
        #endregion





        #region Events


        private bool SaveItem()
        {
            OUserInfo objUser = new OUserInfo();
            objUser.UserId = GetRequestValue<int>("userid", 0);
            objUser.UserName = tbxUserName.Text.Trim();
            objUser.PassWord = tbxPassword.Text.Trim();
            objUser.RealName = tbSelectedDept.Text.Trim();
            objUser.UserType = 2;
            objUser.RoleId = Convert.ToInt32(ddlRoleId.SelectedValue);
            objUser.State = 1;
            objUser.CreateDate = DateTime.Now;
            objUser.Mobile = tbxMobile.Text.Trim();
            objUser.EMail = tbxEmail.Text.Trim();
          //  objUser.PartsEmail = tbxPartsEmail.Text.Trim();
            objUser.Department = hfSelectedDept.Text.Trim();
            objUser.SrvActUserId = Convert.ToInt32(ddlSrvActUserId.SelectedValue);

           // return false;
            return OUserController.Update(objUser);
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
                    

            if (SaveItem())
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
            else
            {
                Alert.Show("修改经销商用户失败！", "提示", MessageBoxIcon.Error);
            }
        }
        #endregion
    }
}